﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crosland.Automotriz.Core;
using System.Data.SqlClient;
using System.Data;
namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsT_Man_Sistema_ModeloDAO
    {

        private AutomotrizEntities ContextoDeObjs = new AutomotrizEntities();


        public bool Insertar(T_MAN_SISTEMA_MODELO Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("T_MAN_SISTEMA_MODELO", Obj);
                ContextoDeObjs.SaveChanges();
            }            
            catch (Exception ex)
            {
                Globales.msjAplicacion = string.Empty;
                Globales.msjAplicacion = ex.InnerException.Message;

                return false;
            }
            return true;
        }


        public bool Eliminar(T_MAN_SISTEMA_MODELO obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAN_SISTEMA_MODELO", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (SqlException ex) {
                Globales.msjSql = ex.Message;
            
            }

            catch (Exception ex)
            {
                Globales.msjAplicacion = ex.Message;
                return false;
            }
            return true;
        }

        public bool Modificar(T_MAN_SISTEMA_MODELO obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAN_SISTEMA_MODELO", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;

            }
            return true;
        }




        public List<T_MAN_SISTEMA_MODELO> consulta_lista(int id_modelo,int id_sistema) {

            List<T_MAN_SISTEMA_MODELO> list = new List<T_MAN_SISTEMA_MODELO>();

            try
            {


                var q = from c in ContextoDeObjs.T_MAN_SISTEMA_MODELO
                        where c.ID_MODELO==id_modelo && c.ID_SISTEMA==id_sistema
                        select c;

                list = q.ToList();
            }
            catch
            {
                
          
            }


            return list;
        
        }

        public List<T_MAN_SISTEMA_MODELO> lista()
        {

            List<T_MAN_SISTEMA_MODELO> list = new List<T_MAN_SISTEMA_MODELO>();

            try
            {


                var q = from c in ContextoDeObjs.T_MAN_SISTEMA_MODELO
                        select c;

                list = q.ToList();
            }
            catch
            {


            }


            return list;

        }

        public bool ActualizaDescripcionImagen() {

            try
            {
                ContextoDeObjs.ExecuteStoreCommand("update t_man_sistema_modelo set DES_IMAGEN='' where DES_IMAGEN is null");
                return true;
            }
            catch 
            {
                return false;
            }

        }


    }
}
